package com.ymatou.iapi.godmch.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.applyAgentBean;
import com.ymatou.iapi.godmch.parameter.approveAgentBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.reApplyAgentBean;
import com.ymatou.iapi.godmch.parameter.rejectAgentBean;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.createagentCall;
import com.ymatou.iapi.godmch.service.reApplyAgentCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.country_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
/**
*重新申请代理
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_reApplyAgent{
	private static reApplyAgentBean bean;
	private static reApplyAgentCall call;
	
	agent_typeWapper agentTypeWapper=new agent_typeWapper();
	mch_infoWapper infoWapper=new mch_infoWapper();
	country_infoWapper country_infoWapper=new country_infoWapper();
	mch_relationWapper relationWapper=new mch_relationWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("重新申请代理");
	}

	@Before
	public void caseUp() {
		bean=new reApplyAgentBean();
		call=new reApplyAgentCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_reApplyAgent_001() {
		Logger.start(true,"正例-重新申请代理-拒绝-重新申请");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);		
			//申请代理
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyBean.setMchId(createCall.getmchId());
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//拒绝
			rejectAgentBean rejectBean=GodmchManageCallService.initRejectAgentBean();
			rejectBean.setMchId(createCall.getmchId());
			rejectBean.setAgentId(applyCall.getAgentId());
			GodmchManageCallService.rejectAgentCall(rejectBean);
			//重新申请
			bean.setMchId(createCall.getmchId());
			bean.setAgentId(applyCall.getAgentId());
			call.setData(bean);
			call.callService();
			
			//验证
		    Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals("请求成功", call.getMessage(), "验证Message");
			List<Integer> mchIds=new ArrayList<Integer>();
			mchIds.add(applyCall.getAgentId());
			mch_info  info=infoWapper.selectbyMchIds(mchIds).get(0);
			
			Logger.verifyEquals(true, info.getMchId()>0, "验证数据库mch_info新增一条数据");
			//mch_info
			Logger.verifyEquals(1, info.getAgent(), "验证数据库mch_info中的agent");
			Logger.verifyEquals(1, info.getChannels(), "验证数据库mch_info中的Channels");
			Logger.verifyEquals(-3, info.getState(), "验证数据库mch_info中的State");
			
			//mch_relation
			mch_relation relation=relationWapper.selectByPrimaryKey(applyCall.getAgentId(), applyBean.getMchId()).get(0);
			Logger.verifyEquals(-2, relation.getState(), "验证state");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_reApplyAgent_001_1() {
		Logger.start(true,"正例-重新申请代理-通过-解绑-重新申请");
		try {
			
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);		
			//申请代理
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyBean.setMchId(createCall.getmchId());
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//创建代理类型
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			//审核通过
			approveAgentBean approveBean=GodmchManageCallService.InitApproveAgentBean();
			approveBean.setMchId(applyBean.getMchId());
			approveBean.setAgentId(applyCall.getAgentId());
			approveBean.setAgentType(list.get(0).getAgentType());
			GodmchManageCallService.ApproveAgentCall(approveBean);
			//解绑
			GodmchManageCallService.InitremovemchForAgent(applyBean.getMchId(),applyCall.getAgentId());
			//重新申请
			bean.setMchId(createCall.getmchId());
			bean.setAgentId(applyCall.getAgentId());
			call.setData(bean);
			call.callService();
			
			//验证
		    Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals("请求成功", call.getMessage(), "验证Message");
			List<Integer> mchIds=new ArrayList<Integer>();
			mchIds.add(applyCall.getAgentId());
			mch_info  info=infoWapper.selectbyMchIds(mchIds).get(0);
			
			Logger.verifyEquals(true, info.getMchId()>0, "验证数据库mch_info新增一条数据");
			//mch_info
			Logger.verifyEquals(1, info.getAgent(), "验证数据库mch_info中的agent");
			Logger.verifyEquals(1, info.getChannels(), "验证数据库mch_info中的Channels");
			Logger.verifyEquals(0, info.getState(), "验证数据库mch_info中的State");
			
			//mch_relation
			mch_relation relation=relationWapper.selectByPrimaryKey(applyCall.getAgentId(), applyBean.getMchId()).get(0);
			Logger.verifyEquals(-2, relation.getState(), "验证state");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_reApplyAgent_001_2() {
		Logger.start(true,"正例-重新申请代理-通过-重新申请-失败");
		try {
			
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);		
			//申请代理
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyBean.setMchId(createCall.getmchId());
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//创建代理类型
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			//审核通过
			approveAgentBean approveBean=GodmchManageCallService.InitApproveAgentBean();
			approveBean.setMchId(applyBean.getMchId());
			approveBean.setAgentId(applyCall.getAgentId());
			approveBean.setAgentType(list.get(0).getAgentType());
			GodmchManageCallService.ApproveAgentCall(approveBean);
			//解绑
			//GodmchManageCallService.InitremovemchForAgent(applyBean.getMchId(),applyCall.getAgentId());
			//重新申请
			bean.setMchId(createCall.getmchId());
			bean.setAgentId(applyCall.getAgentId());
			call.setData(bean);
			call.callService();
			
//			//验证
//		    Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
//			Logger.verifyEquals("请求成功", call.getMessage(), "验证Message");
//			List<Integer> mchIds=new ArrayList<Integer>();
//			mchIds.add(applyCall.getAgentId());
//			mch_info  info=infoWapper.selectbyMchIds(mchIds).get(0);
//			
//			Logger.verifyEquals(true, info.getMchId()>0, "验证数据库mch_info新增一条数据");
//			//mch_info
//			Logger.verifyEquals(1, info.getAgent(), "验证数据库mch_info中的agent");
//			Logger.verifyEquals(1, info.getChannels(), "验证数据库mch_info中的Channels");
//			Logger.verifyEquals(0, info.getState(), "验证数据库mch_info中的State");
//			
//			//mch_relation
//			mch_relation relation=relationWapper.selectByPrimaryKey(applyCall.getAgentId(), applyBean.getMchId()).get(0);
//			Logger.verifyEquals(-2, relation.getState(), "验证state");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_reApplyAgent_002() {
		Logger.start(true,"正例-重新申请代理-mchId不存在");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);		
			//申请代理
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyBean.setMchId(createCall.getmchId());
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//创建代理类型
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			bean.setMchId(121231);
			bean.setAgentId(applyCall.getAgentId());
			call.setData(bean);
			call.callService();		
			
			//验证
		    Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals("代理商不存在", call.getMessage(), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_reApplyAgent_003() {
		Logger.start(true,"正例-重新申请代理-mchId为空");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);		
			//申请代理
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyBean.setMchId(createCall.getmchId());
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//创建代理类型
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			bean.setMchId(null);
			bean.setAgentId(applyCall.getAgentId());
			call.setData(bean);
			call.callService();		
			
			//验证
		    Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("参数异常"), "验证Message");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_reApplyAgent_004() {
		Logger.start(true,"正例-重新申请代理-agentId不存在");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);		
			//申请代理
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyBean.setMchId(createCall.getmchId());
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//创建代理类型
			createagentBean agentBean=GodmchManageCallService.InitCreateAgentTypeBean(applyBean.getMchId());
			createagentCall agentCall=GodmchManageCallService.CreateAgentTypeCall(agentBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			
			//审核通过
			approveAgentBean approveBean=GodmchManageCallService.InitApproveAgentBean();
			approveBean.setMchId(applyBean.getMchId());
			approveBean.setAgentId(applyCall.getAgentId());
			approveBean.setAgentType(list.get(0).getAgentType());
			GodmchManageCallService.ApproveAgentCall(approveBean);
			//解绑
			GodmchManageCallService.InitremovemchForAgent(applyBean.getMchId(),applyCall.getAgentId());
			//重新申请
			bean.setMchId(createCall.getmchId());
			bean.setAgentId(1111);
			call.setData(bean);
			call.callService();
			
			//验证
		    Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals("代理商不存在", call.getMessage(), "验证Message");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_reApplyAgent_005() {
		Logger.start(true,"正例-重新申请代理-agentId为空");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);		
			//申请代理
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyBean.setMchId(createCall.getmchId());
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//拒绝
			rejectAgentBean rejectBean=GodmchManageCallService.initRejectAgentBean();
			rejectBean.setMchId(createCall.getmchId());
			rejectBean.setAgentId(applyCall.getAgentId());
			//重新申请
			bean.setMchId(createCall.getmchId());
			bean.setAgentId(null);
			call.setData(bean);
			call.callService();
			
			//验证
		    Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("参数异常"), "验证Message");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_reApplyAgent_006() {
		Logger.start(true,"正例-重新申请代理-mchId与agentId不存在代理关系");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);		
			//申请代理
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyBean.setMchId(createCall.getmchId());
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//拒绝
			rejectAgentBean rejectBean=GodmchManageCallService.initRejectAgentBean();
			rejectBean.setMchId(createCall.getmchId());
			rejectBean.setAgentId(applyCall.getAgentId());
			//重新申请
			bean.setMchId(createCall.getmchId());
			bean.setAgentId(500002975);
			call.setData(bean);
			call.callService();
			
			//验证
		    Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("代理商不存在"), "验证Message");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}